<?php
/**
 * This file houses the Router class.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Framework
 * @subpackage Library
 */

/**
 * The Router class is used to pre-define routes or use the URL to return the controller and action to use.
 *
 * @package Framework
 * @subpackage Library
 */
class Router {

	/**
	 * A single instance of the Router class.
	 * Implementation of the Singleton design pattern.
	 *
	 * @var Router
	 */
	private static $instance;

	/**
	 * Container for pre-defined routes.
	 *
	 * @var array
	 */
	private $routes;

	/**
	 * Object constructor.
	 *
	 * @return Router
	 */
	private function __construct() {
		$this->routes = array();
	}

	/**
	 * Returns a single instance of the Router class.
	 * Implementation of the Singleton design pattern.
	 *
	 * @return Router
	 */
	public static function getInstance() {
		if (self::$instance === null) {
			self::$instance = new Router();
		}
		return self::$instance;
	}

	/**
	 * Indicates what URL should route to which controller and action.  FOr example, a URL like '/about_me' could route to /pages/about_me.
	 * Note that any parameters after the defined URL will not be transferred to the controller like normal.
	 *
	 * @param string $url
	 * @param string $protocol
	 * @param string $controller
	 * @param string $action
	 *
	 * @return void
	 */
	public function setRoute($url, $protocol, $controller, $action) {
		$this->routes[$url] = array ( 'protocol' => $protocol, 'controller' => $controller, 'action' => $action );
	}

	/**
	 * Checks defined routes and the actual URL to return an array with the controller, action, and any URL parameters.
	 *
	 * @return array associative array with keys 'controller', 'action', and 'params'
	 */
	public function getRoute() {
		$requestedUrl = filter_input(INPUT_GET, 'url', FILTER_SANITIZE_URL);
		if ($requestedUrl === null || $requestedUrl === false) {
			$requestedUrl = '/';
		}
		if (array_key_exists($requestedUrl, $this->routes)) {
			$route = $this->routes[$requestedUrl];
			$route['params'] = array();
		} else {
			$params = explode('/', $requestedUrl);
			$protocol = array_shift($params);
			$controller = array_shift($params);
			$action = array_shift($params);
			$route = array ( 'protocol' => $protocol, 'controller' => $controller, 'action' => $action, 'params' => $params );
		}
		return $route;
	}

}
